package com.kqkj.dyrule.marketing.buffer;

import com.kqkj.dyrule.marketing.beans.BufferData;
import com.kqkj.dyrule.marketing.utils.ConnectionUtils;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * 缓存管理器
 */
public class BufferManagerImpl implements BufferManager {
    Jedis jedis;
    public BufferManagerImpl(){
        jedis = ConnectionUtils.getRedisConnection();

    }

    @Override
    public BufferData getDataFromBuffer(String bufferKey) {
        Map<String, String> valueMap = jedis.hgetAll(bufferKey);
        String[] split = bufferKey.split(":");
        return new BufferData(split[0],split[1],valueMap);
    }

    @Override
    public boolean putDataToBuffer(BufferData bufferData) {
        String hset = jedis.hmset(bufferData.getDeviceId() + ":" + bufferData.getCacheId(), bufferData.getValueMap());
        return "OK".equals(hset);
    }

    @Override
    public boolean putDataToBuffer(String bufferKey,Map<String,String> value) {
        String hset = jedis.hmset(bufferKey, value);

        return "OK".equals(hset);
    }

    public void delBufferEntry(String bufferKey, String key) {
        jedis.hdel(bufferKey,key);
    }
}
